﻿namespace SS.WPF.Framework.Localization
{
    using System;
    using System.Windows;

    /// <summary>
    /// The class is used to manage event that are raised from TranslationManager by using WeakEvents pattern
    /// <para>
    /// Every time user change language, the TranslationManager raises an event to listenr to process
    /// </para>
    /// </summary>
    public class LanguageChangedEventManager : WeakEventManager
    {
        /// <summary>
        /// Get instance of Event manager
        /// </summary>
        private static LanguageChangedEventManager CurrentManager
        {
            get
            {
                var managerType = typeof(LanguageChangedEventManager);
                var manager = (LanguageChangedEventManager)GetCurrentManager(managerType);
                if (manager == null)
                {
                    manager = new LanguageChangedEventManager();
                    SetCurrentManager(managerType, manager);
                }
                return manager;
            }
        } 

        /// <summary>
        /// Adds the provided listener to the provided source for the event being managed.
        /// </summary>
        /// <param name="source">Event source</param>
        /// <param name="listener">Event listener</param>
        public static void AddListener(TranslationManager source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedAddListener(source, listener);
        }

        /// <summary>
        /// Removes a previously added listener from the provided source.
        /// </summary>
        /// <param name="source">Event source</param>
        /// <param name="listener">Event listener</param>
        public static void RemoveListener(TranslationManager source, IWeakEventListener listener)
        {
            CurrentManager.ProtectedRemoveListener(source, listener);
        }

        /// <summary>
        /// The method that is used to handle Event from Event source
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnLanguageChanged(object sender, EventArgs e)
        {
            DeliverEvent(sender, e);
        }

        /// <summary>
        /// When overridden in a derived class, starts listening for the event being
        /// managed. After System.Windows.WeakEventManager.StartListening(System.Object)
        //  is first called, the manager should be in the state of calling System.Windows.WeakEventManager.DeliverEvent(System.Object,System.EventArgs)
        //  or System.Windows.WeakEventManager.DeliverEventToList(System.Object,System.EventArgs,System.Windows.WeakEventManager.ListenerList)
        //  whenever the relevant event from the provided source is handled.
        /// </summary>
        /// <param name="source">The source to begin listening on.</param>
        protected override void StartListening(object source)
        {
            var manager = (TranslationManager)source;
            manager.LanguageChanged += OnLanguageChanged;
        }

        /// <summary>
        /// When overridden in a derived class, stops listening on the provided source
        //  for the event being managed.
        /// </summary>
        /// <param name="source">The source to stop listening on.</param>
        protected override void StopListening(Object source)
        {
            var manager = (TranslationManager)source;
            manager.LanguageChanged -= OnLanguageChanged;
        }
    }
}
